package com.cadre.web.controller.officer;

import com.alibaba.fastjson.JSON;
import com.cadre.common.utils.R;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.vo.ESUtil;
import com.cadre.system.service.A01Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

@RequestMapping({"/test"})
@RestController
/* loaded from: TestController.class */
public class TestController {
    @Autowired
    RestHighLevelClient client;
    @Resource
    private A01Service a01Service;

    @GetMapping({"/createEs"})
    public R createEs() throws IOException {
        System.out.println(this.client.indices().create(new CreateIndexRequest(ESUtil.a01Index), RequestOptions.DEFAULT).isAcknowledged());
        return R.ok();
    }

    @GetMapping({"/getEsData"})
    public void saveListEs() throws IOException {
        for (A01 a01 : this.a01Service.selectEs()) {
            System.out.println(this.client.index(new IndexRequest(ESUtil.a01Index).id(a01.getFlowid()).source(JSON.toJSONString(a01), XContentType.JSON), RequestOptions.DEFAULT).getId());
        }
    }

    @GetMapping({"/searcEsById"})
    public R searcEsById() throws IOException {
        GetResponse response = this.client.get(new GetRequest(ESUtil.a01Index, "d0a07afb76544a03a2bfd6e1d7bd6185"), RequestOptions.DEFAULT);
        System.out.println(response.getSourceAsMap());
        return R.ok().put("value", (Object) response.getSourceAsMap());
    }

    @GetMapping({"/searcEsData"})
    public void searcEsData(@RequestParam String text) throws IOException {
        SearchRequest searchRequest = new SearchRequest(new String[]{ESUtil.a01Index});
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.multiMatchQuery("好啊", new String[]{"a0101", "a0204", "a92List", "a91List", "a17List", "a26List", "a22List", "a16List", "a04List", "a19List", "a79List"})));
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(sourceBuilder);
        SearchHits hits = this.client.search(searchRequest, RequestOptions.DEFAULT).getHits();
        List<A01> a01s = new ArrayList<>();
        Iterator it = hits.iterator();
        while (it.hasNext()) {
            a01s.add(useObjectMapper(((SearchHit) it.next()).getSourceAsMap()));
        }
        Stream<A01> stream = a01s.stream();
        PrintStream printStream = System.out;
        printStream.getClass();
        stream.forEach((v1) -> {
            System.out.println(v1);;
        });
    }

    @GetMapping({"/searcEsByValue"})
    public R searcEsByValue() throws IOException {
        SearchRequest request = new SearchRequest(new String[]{ESUtil.a01Index});
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.from(0);
        builder.size(100);
        builder.query(QueryBuilders.queryStringQuery("自行").analyzeWildcard(false));
        builder.timeout(TimeValue.timeValueSeconds(1));
        request.source(builder);
        SearchHits hits = this.client.search(request, RequestOptions.DEFAULT).getHits();
        List<A01> a01s = new ArrayList<>();
        Iterator it = hits.iterator();
        while (it.hasNext()) {
            a01s.add(useObjectMapper(((SearchHit) it.next()).getSourceAsMap()));
        }
        Stream<A01> stream = a01s.stream();
        PrintStream printStream = System.out;
        printStream.getClass();
        stream.forEach((v1) -> {
            System.out.println(v1);;
        });
        return R.ok().put("a01s", (Object) a01s);
    }

    private static A01 useObjectMapper(Map<String, Object> map) {
        return (A01) new ObjectMapper().convertValue(map, A01.class);
    }
}
